home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / NNUTL101.ZIP / NNSIM1 / NNSIM1.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-30  |  5.3 KB  |  111 lines

  1. /*--------------------------------------------------------------------------*
  2.  * Gregory Stevens                                                   7/1/93 *
  3.  *                                NNSIM1.C                                  *
  4.  *    (Generalized Simulator: Supervised,Feed Forward,Back Propagation)     *
  5.  *                                                                          *
  6.  *   This is a generalized simulator for a supervised feed-forward neural   *
  7.  * with back-propagation.  It uses the nn*.c series.                        *
  8.  * For this file, the following parameters in the following files should be *
  9.  * set to comply with the input file and desired net configuration:         *
  10.  *      NNPARAMS.C : INPUT_LAYER_SIZE                                       *
  11.  *                   OUTPUT_LAYER_SIZE                                      *
  12.  *                   NUM_HIDDEN_LAYERS                                      *
  13.  *                   HL_SIZE_#                                              *
  14.  *                                                                          *
  15.  *      NNINPUTS.C : NUM_PATTERNS                                           *
  16.  *                                                                          *
  17.  *      NNSTRUCT.C : InitNet()   (generally, all nodes should be logistic)  *
  18.  *                                                                          *
  19.  *      NNBKPROP.C : EPSILON 0.25  (recommended...this is what I used)      *
  20.  *                                                                          *
  21.  *   There should be 3 data files: nninputs.dat, nnoutput.dat, nnintest.dat *
  22.  *   These correspond to the training inputs, desired output for training   *
  23.  * inputs, and novel inputs to test for generalization.                     *
  24.  *                                                                          *
  25.  *   NOTE: The last file is only necessary if INTEST is set to 1.           *
  26.  *                                                                          *
  27.  *--------------------------------------------------------------------------*/
  28. #include "nnbkprop.c"                /* to chain it to the nn*.c utilities  */
  29.  
  30. #define IN_TEST 1                    /* whether generalization tests should */
  31.                                      /* be made (Boolean)                   */
  32. #define NUM_ITS 200                  /* iterations before it stops training */
  33.  
  34. void main()
  35. {
  36.   int Pattern;                          /* for looping through patterns   */
  37.   int Layer;                            /* for looping through layers     */
  38.   int LCV;                              /* for looping training sets      */
  39.   NNETtype Net;                         /* for the network itself         */
  40.   PATTERNtype InPatterns, OutPattern;   /* for the training patterns      */
  41.  
  42.   Net = InitNet( NUMNODES );           /* initializes the network        */
  43.   InPatterns = InitInPatterns(0);       /* loads input patterns from file */
  44.   OutPattern = InitOutPatterns();       /* loads output patterns from file*/
  45.  
  46.   printf("\n\n\n\n\n");                 /* gets screen ready for output   */
  47.  
  48.   printf( "BEGINNING TRAINING:\n\n" );
  49.  
  50.   for (LCV=0; (LCV < NUM_ITS); ++LCV)   /* loop through a training set    */
  51.     {
  52.       for (Pattern=0; (Pattern<NUM_PATTERNS); ++Pattern)  /* each pattern */
  53.          {
  54.             /* FORWARD PROPAGATION */
  55.             Net = UpDateInputAct( InPatterns, Pattern, Net );
  56.             for (Layer=1; (Layer<NUMLAYERS); ++Layer)
  57.               {
  58.                  Net = UpDateLayerAct( Net, Layer );
  59.               }
  60.  
  61.             /* OUTPUT PRINTS */
  62.             /*    NOTE: The last number in DisplayLayer() will need to be */
  63.             /*          adjusted to format different size input layers.   */
  64.  
  65.             DisplayLayer( Net, 0, 8 );             /* display input layer */
  66.             printf( "   " );
  67.             DisplayLayer( Net, (NUMLAYERS-1), 1);  /* display output layer*/
  68.             printf( "\n" );                        /* new line            */
  69.  
  70.                                                    /*it is possible to    */
  71.                                                    /*display hidden layers*/
  72.  
  73.             /* BACKWARD PROPAGATION */
  74.             Net = UpDateWeightandThresh( Net, OutPattern, Pattern );
  75.          }
  76.  
  77.       printf( "\n\n" );                            /* prepare for next set*/
  78.  
  79.       if ( LCV > (NUM_ITS-10) )
  80.         {
  81.            getc(stdin);           /* pause inbetween training epochs */
  82.         }
  83.     }
  84.  
  85.   if (IN_TEST==1)
  86.     {
  87.       InPatterns = InitInPatterns(1);          /* Loads test input patterns */
  88.  
  89.       printf( "BEGINNING PATTERN TESTING:\n\n" );
  90.  
  91.       for (Pattern=0; (Pattern<NUM_PATTERNS); ++Pattern)
  92.          {
  93.            Net = UpDateInputAct( InPatterns, Pattern, Net ); /* load inputs */
  94.  
  95.            for (Layer=1; (Layer<NUMLAYERS); ++Layer)         /* run through */
  96.               {
  97.                  Net = UpDateLayerAct( Net, Layer );
  98.               }
  99.  
  100.             /* OUTPUT PRINTS */
  101.             DisplayLayer( Net, 0, 8 );             /* display input layer */
  102.             printf( "   " );
  103.             DisplayLayer( Net, (NUMLAYERS-1), 1 ); /* display output layer*/
  104.             printf( "\n" );                        /* new line            */
  105.          }
  106.  
  107.  
  108.       getc(stdin);
  109.     }
  110. }
  111.